package com.iplatform.commons;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.github.pagehelper.util.StringUtil;

import feign.RequestInterceptor;
import feign.RequestTemplate;

@Configuration
public class FeignHeaderInterceptor implements RequestInterceptor{

	@Override
	public void apply(RequestTemplate template) {
		ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if(attributes==null){
            return;
        }
        HttpServletRequest request = attributes.getRequest();
        String access_token = "";
        String auth = request.getHeader("Authorization");
        if(StringUtil.isNotEmpty(request.getParameter("access_token"))) {
        	access_token = request.getParameter("access_token");
        	//添加token
            template.header("Authorization", OAuth2AccessToken.BEARER_TYPE +" "+request.getParameter("access_token"));
        }else if(StringUtil.isNotEmpty(auth)) {
        	//添加token
            template.header("Authorization", auth);
        }

	}

}
